// xc/components/advert/pro.js
// var actclub_barter = require('../../model/club_barter')
// var actclub_barter = require('../../../xc_lshop/club/model/club_barter')
var barter_order = require('../../../model/barter_order')

Component({
  options: {
    addGlobalClass: true,
    multipleSlots: true
  },
  /**
   * 组件的属性列表
   */
  properties: {
    clubid: Number,
    uid: Number,
    level: {
      type: Number,
      value: 1
    },
    bclass: {
      type: Array,
      value: []
    },
    sclass: {
      type: Object,
      value: {}
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    classshow: false,
    /*表单验证是否通过数据*/
    vildfield: {
      price: true,
      name: true,
      bpid: true,
      spid: true,
      num: true
    },
    /* 表单提交的数据*/
    formdata: {},
    fileList: [],
  },

  /**
   * 组件的方法列表
   */
  methods: {
    onChange(e) {
      console.log(e)
      this.setData({
        "formdata.isonline":e.detail
      })
    },
    onClosepopup() {
      this.setData({
        classshow: false
      })
    },
    onconfirmclass(e) {
      var value = e.detail.value;
      var update = {
        classshow: false
      }
      if (this.data.op == 'bclass') {
        update['formdata.bpid'] = value.id
      } else {
        update['formdata.spid'] = value.id
      }
      this.setData(update)
      if (this.data.op == 'bclass') {
        this.checkRule("bpid")
      } else {
        this.checkRule("spid")
      }
    },
    chooseclass(e) {
      console.log(e)
      var update = {
        classshow: true
      }
      var op = e.currentTarget.dataset.op;
      update['op'] = op
      var bpid = this.data.formdata.bpid
      if (op == 'bclass') {
        update['proclass'] = this.data.bclass
      } else {
        console.log("this.data.sclass[bpid]")
        console.log(this.data.sclass)
        console.log(bpid)
        console.log(this.data.sclass[bpid])

        if (this.data.sclass[bpid]) {
          update['proclass'] = this.data.sclass[bpid]

        } else {
          update['proclass'] = []

        }

      }
      this.setData(update)
    },
    // 表单提交
    formSubmit: function (e) {
      this.data.formdata.uid = this.data.uid
      var that = this;
      var valid = this.checkForm();
      console.log(valid)
      if (valid) {
        barter_order.applyform(that.data.formdata, that.data.clubid).then(function (res) {
          if (res["status"] == 1) {
            that.selectComponent("#examstatus").init(res["examstatus"])
          } else {
            that.error(res["message"])
          }
        })
      }
      console.log(valid)
    },
    //验证规则
    checkRule(field) {
      var updatemodel = {};
      if (field == 'spid' || field == 'bpid') {
        if (field == 'spid' && this.data.level == 1) {
          updatemodel['vildfield.' + field] = true;
        } else {
          updatemodel['vildfield.' + field] = this.data.formdata[field] > 0 ? true : false;
        }
      } else
      if (field == "sex") {
        updatemodel['vildfield.' + field] = (this.data.formdata[field] == 1 || this.data.formdata[field] == 2) ? true : false;
      } else {
        updatemodel['vildfield.' + field] = this.data.formdata[field] ? true : false;
      }
      this.setData(updatemodel)
    },
    //验证表单
    checkForm: function (field) {
      var error = true;
      if (field) {
        this.checkRule(field)
      } else {
        ["name", "price"].forEach(item => {
          this.checkRule(item)
          if (!this.data.vildfield[item]) {
            error = false;
          }
        })
      }
      return error;
    },
    //监听input输入
    watchInput: function (e) {
      var dataset = e.target.dataset
      this.data.formdata[dataset.field] = e.detail
      this.checkForm(dataset.field)
    },
    savefilefunc(file) {
      var app = getApp()
      var that = this
      app.toolv2.uploadFile(file.path).then(function (item) {
        var fileList = that.data.fileList;
        fileList.push({
          ...file,
          url: item.obj.fullpath
        });
        console.log(fileList)
        that.setData({
          fileList
        });
        that.data.formdata.imglist = [];
        fileList.forEach(item => {
          that.data.formdata.imglist.push(item.url)
        })
      })

    },
    afterRead(event) {
      console.log("event")

      console.log(event)

      var that = this
      const {
        file
      } = event.detail

      if (Object.prototype.toString.call(file) === '[object Array]') {
        file.forEach(item => {

          that.savefilefunc(item)
        })
      } else {
        that.savefilefunc(file)
      }
    },


    afterdelete(e) {

      var that = this;
      var fileList = that.data.fileList
      var index = e.detail.index;

      fileList.splice(index, 1);

      this.setData({
        fileList: fileList,
      });
      var imglist = [];
      fileList.forEach(item => {
        imglist.push(item.url)
      })
      that.data.formdata.imglist = imglist

    },
    //上传图片--传图--获取url
    getUrlParam(url, name) {
      var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象  
      var r = url.split('?')[1].match(reg); //匹配目标参数  
      if (r != null) return unescape(r[2]);
      return null; //返回参数值  
    },
    error(message, back) {
      const app = getApp()
      var that = this;
      var homeurl = app.globalData.configdata.basic.homeurl
      wx.showModal({
        title: "错误",
        content: message,
        showCancel: false,
        success(res) {
          if (res.confirm) {
            if (back) {
              that.pageback(homeurl)
            }
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    },
    pageback(homeurl, isnav = false) {
      if (!homeurl) {
        const app = getApp()

        if (app.globalData.configdata) {
          homeurl = app.globalData.configdata.basic.homeurl;
        } else {
          homeurl = '/xc_lshop/index/index';
        }
      }
      var pages = getCurrentPages();

      if (isnav) {
        wx.reLaunch({
          url: homeurl,
        })
      } else {
        if (pages[pages.length - 2]) {
          wx.navigateBack({
            delta: 1
          })
        } else {
          wx.reLaunch({
            url: homeurl,
          })
        }
      }

    },
    btnop(e) {
      var op = e.detail.op;
      if (op == 1) {
        var formdata = this.data.formdata;
        formdata['name'] = "";
        formdata['price'] = "";
        formdata['message'] = "";
        formdata['imglist'] = [];

        this.setData({
          formdata,
          fileList: []
        })
      } else {
        this.pageback()
      }

    },
  }
})